You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The streaming XML tool parser treats every </codebuff_tool_call> as a frame boundary, including one inside a valid JSON string. A tool argument such as "content": "</codebuff_tool_call>" therefore ends the frame early: JSON parsing fails, the tool call disappears, and the remaining arguments become ordinary text.
This affects the text-to-tool path used by processStreamWithTools. A local dispatcher fixture reproduces a missing file write from otherwise complete arguments.
Changes
Track JSON string and backslash-escape state across chunks. Recognize the closing tag only outside a string, while keeping the existing body buffer and partial-tag handling.
Strengthen one existing parser test with a complete closing tag, escaped quotes, and a trailing backslash. It retains the previous partial-tag input and checks both the tool arguments and filtered text for chunk sizes 1–24.
Verification
The streaming parser, batch parser, tool-stream, and duplicate-text suites: main with the changed case has 46 passed / 1 failed; the patch has 47 passed / 0 failed, 428 assertions.
A local processStreamWithTools probe using three-character chunks invokes its injected execution callback once and writes the complete literal-tag content. Main invokes it zero times. The unchanged newline-framed batch parser accepts the same arguments on both versions.
Agent-runtime and SDK typechecks pass with native TypeScript 7.0.2. Frozen-lockfile install passes.
ESLint: 0 errors, 4 local resolver warnings. Whole-file Prettier checks still report existing differences; comparing with main finds no new formatting differences.
SDK and binary builds remain blocked by the existing declaration-generator dependency: its locked native TypeScript 7.0.2 has no ts.sys. No build or binary-smoke pass is claimed.
Notes / Scope
Two agent-runtime files, including two fields on the internal parser state. No dependency or XML wire-format changes.
An unterminated JSON string leaves the frame open; this patch does not guess a boundary or repair malformed arguments. Native tool-call handling and the newline-framed batch parser are unchanged.
Validation ran on Windows with Bun 1.3.14 and local fixtures. Live models, other operating systems, the full tool authorization chain, and the full monorepo suite were not run.
Good catch and a clean, minimal fix. The root cause is accurately diagnosed: parseStreamChunk's text.indexOf(endToolTag) in stream-xml-parser.ts matches </codebuff_tool_call> even when it appears inside a JSON string value, truncating the frame and corrupting the tool call. The new findToolCallEnd helper walks the chunk character-by-character, tracking inJsonString/jsonEscapeNext on the persistent parser state so the scan correctly carries over string/escape context across chunk boundaries (verified by the trailing-backslash case in the updated test). Outside of a JSON string the behavior is unchanged (text.startsWith(endToolTag, i) mirrors the old indexOf semantics), so this shouldn't regress existing callers.
The test update is solid - it keeps the original partial-tag coverage, adds an embedded closing tag plus escaped quote and trailing backslash, and checks both toolCalls and surrounding text across chunk sizes 1-24, which is exactly the kind of boundary-sensitive coverage this parser needs.
A couple of things worth double-checking before landing: (1) the same false-positive risk likely exists for the opening<codebuff_tool_call> tag detection when it appears inside preceding literal text that happens to contain quotes - out of scope here, but worth a follow-up issue; (2) no handling for malformed/unterminated JSON strings is claimed or needed, which is reasonable since the PR explicitly scopes that out.
Small, self-contained change confined to packages/agent-runtime, with a clear repro and before/after test counts. This looks straightforward to port.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem / Why
The streaming XML tool parser treats every
</codebuff_tool_call>as a frame boundary, including one inside a valid JSON string. A tool argument such as"content": "</codebuff_tool_call>"therefore ends the frame early: JSON parsing fails, the tool call disappears, and the remaining arguments become ordinary text.This affects the text-to-tool path used by
processStreamWithTools. A local dispatcher fixture reproduces a missing file write from otherwise complete arguments.Changes
Track JSON string and backslash-escape state across chunks. Recognize the closing tag only outside a string, while keeping the existing body buffer and partial-tag handling.
Strengthen one existing parser test with a complete closing tag, escaped quotes, and a trailing backslash. It retains the previous partial-tag input and checks both the tool arguments and filtered text for chunk sizes 1–24.
Verification
processStreamWithToolsprobe using three-character chunks invokes its injected execution callback once and writes the complete literal-tag content. Main invokes it zero times. The unchanged newline-framed batch parser accepts the same arguments on both versions.ts.sys. No build or binary-smoke pass is claimed.Notes / Scope
Two agent-runtime files, including two fields on the internal parser state. No dependency or XML wire-format changes.
An unterminated JSON string leaves the frame open; this patch does not guess a boundary or repair malformed arguments. Native tool-call handling and the newline-framed batch parser are unchanged.
Validation ran on Windows with Bun 1.3.14 and local fixtures. Live models, other operating systems, the full tool authorization chain, and the full monorepo suite were not run.